package com.example.mywebview

import android.os.Bundle
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Button
import android.widget.LinearLayout
import android.widget.ProgressBar
import androidx.activity.ComponentActivity
import androidx.core.view.isInvisible
import androidx.core.view.isVisible


class MainActivity : ComponentActivity() {
    private var mWebView: WebView? = null
    private var mProgressBar: ProgressBar? = null
    private var mErrorLayout: LinearLayout? = null
    private var mRetry: Button? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mWebView = findViewById(R.id.webView)
        mProgressBar = findViewById(R.id.progressBar)
        mErrorLayout = findViewById(R.id.error_layout)
        mRetry = findViewById(R.id.retry_button)

        mRetry?.setOnClickListener {
            mErrorLayout?.isVisible = false
            mWebView?.reload()
            mWebView?.isVisible = true
        }

        val wvcc: WebChromeClient = object : WebChromeClient() {
            override fun onReceivedTitle(view: WebView, title: String) {
                super.onReceivedTitle(view, title)
            }

            override fun onProgressChanged(view: WebView, newProgress: Int) {
                if (newProgress == 100) {
                    mProgressBar?.visibility = View.GONE
                } else {
                    if (mProgressBar?.visibility == View.GONE) {
                        mProgressBar?.visibility = View.VISIBLE
                    }
                    mProgressBar?.progress = newProgress
                }
                super.onProgressChanged(view, newProgress)
            }
        }

        // 设置setWebChromeClient对象
        mWebView?.setWebChromeClient(wvcc)
        mWebView?.webViewClient = object : WebViewClient() {
            override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
                super.onReceivedError(view, request, error)
                mErrorLayout?.isVisible = true
                mWebView?.isInvisible = true
            }
        }
        //声明WebSettings子类
        mWebView?.getSettings()?.let { webSettings ->
            //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
            webSettings.javaScriptEnabled = true

            // 若加载的 html 里有JS 在执行动画等操作，会造成资源浪费（CPU、电量）
            // 在 onStop 和 onResume 里分别把 setJavaScriptEnabled() 给设置成 false 和 true 即可
            // 设置自适应屏幕，两者合用
            webSettings.useWideViewPort = true //将图片调整到适合webview的大小
            webSettings.loadWithOverviewMode = true // 缩放至屏幕的大小

            //缩放操作
            webSettings.setSupportZoom(true) //支持缩放，默认为true。是下面那个的前提。
            webSettings.builtInZoomControls = true //设置内置的缩放控件。若为false，则该WebView不可缩放
            webSettings.displayZoomControls = false //隐藏原生的缩放控件

            webSettings.domStorageEnabled = true // 允许 DOM 存储
            webSettings.cacheMode = android.webkit.WebSettings.LOAD_DEFAULT // 默认缓存模式

            webSettings.allowFileAccess = true //设置可以访问文件
            webSettings.javaScriptCanOpenWindowsAutomatically = true //支持通过JS打开新窗口
            webSettings.loadsImagesAutomatically = true //支持自动加载图片
            webSettings.defaultTextEncodingName = "utf-8" //设置编码格式
        }

        mWebView?.loadUrl("https://wecs-gz-tm.ipebg.efoxconn.com/app/")

    }
}